﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using LSUtility;
using MySql.Data.MySqlClient;

namespace LSData
{
    public class MySqlProvider : MySqlDisposable
    {
        #region 内部字段与构造函数
        /// <summary>
        /// 出错信息
        /// </summary>
        public string LastError;

        private readonly MySqlConnection _pConnection;

        public MySqlProvider(string strConnect)
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
            if (strConnect == null)
            {
                LogTool.WriteEntry(@"未取得连接字符串:ConnectionString");
            }

            try
            {
                _pConnection = new MySqlConnection(strConnect);
                _pConnection.Open();
            }
            catch (Exception ex)
            {
                LogTool.WriteEntry("OUP.Data.MySqlProvider.MySqlProvider():" + ex.Message + "--" + strConnect);
                LastError = ex.Message;
            }
        }

        protected override void Free(bool bDispodedByUser)
        {
            //if (pConnection.State == ConnectionState.Open)
            _pConnection.Close();
            base.Free(bDispodedByUser);
        }
        #endregion

        #region GetCommand

        /// <summary>
        /// 获取数据库命令对象
        /// </summary>
        /// <param name="strCommandText">命令文本</param>
        /// <param name="pars">参数数组</param>
        /// <param name="commandType"></param>
        /// <param name="commandTimeout"></param>
        /// <returns>命令对象</returns>
        public MySqlCommand GetCommand(string strCommandText, MySqlParameter[] pars, CommandType commandType, int commandTimeout)
        {
            var objCommand = _pConnection.CreateCommand();

            objCommand.CommandType = commandType;

            if (commandTimeout > 0)
            {
                objCommand.CommandTimeout = commandTimeout;
            }

            if (strCommandText != null)
            {
                objCommand.CommandText = strCommandText;
            }

            if (pars != null)
            {
                foreach (MySqlParameter objPar in pars)
                {
                    objCommand.Parameters.Add(objPar);
                }
            }

            return objCommand;
        }

        public MySqlCommand GetCommand(string strCommandText, int commandTimeout)
        {
            return GetCommand(strCommandText, null, CommandType.Text, commandTimeout);
        }

        public MySqlCommand GetCommand(string strCommandText)
        {
            return GetCommand(strCommandText, null, CommandType.Text, 0);
        }

        public MySqlCommand GetCommand()
        {
            return GetCommand(null, null, CommandType.Text, 0);
        }
        #endregion

        #region ExecuteNonQuery
        /// <summary>
        /// 执行命令行文本
        /// </summary>
        /// <param name="sqlComm">命令对象</param>
        /// <returns>返回受影响行数</returns>
        public int ExecuteNonQuery(MySqlCommand sqlComm)
        {
            try
            {
                if (sqlComm.Connection.State == ConnectionState.Closed)
                    sqlComm.Connection.Open();

                return sqlComm.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                LogTool.WriteEntry("OUP.Data.MySqlProvider.ExecuteNonQuery():" + ex.Message + "--" + sqlComm.CommandText);
                LastError = ex.Message;
                return -1;
            }
        }

        public int ExecuteNonQuery(string strCommandText, MySqlParameter[] pars, CommandType commandType)
        {
            return ExecuteNonQuery(GetCommand(strCommandText, pars, commandType, 0));
        }

        public int ExecuteNonQuery(string strCommandText)
        {
            return ExecuteNonQuery(GetCommand(strCommandText));
        }

        public int ExecuteNonQuery(string strCommandText, int commandTimeout)
        {
            return ExecuteNonQuery(GetCommand(strCommandText, commandTimeout));
        }
        #endregion

        #region ExecuteScalar
        /// <summary>
        /// 执行命令,返回某个值
        /// </summary>
        /// <param name="sqlComm"></param>
        /// <returns></returns>
        public string ExecuteScalar(MySqlCommand sqlComm)
        {
            string scalar = string.Empty;
            try
            {
                using (sqlComm)
                {
                    if (sqlComm.Connection.State == ConnectionState.Closed)
                        sqlComm.Connection.Open();

                    object obj = sqlComm.ExecuteScalar();
                    if (obj != DBNull.Value && obj != null)
                        scalar = obj.ToString();
                }
            }
            catch (Exception ex)
            {
                LogTool.WriteEntry("OUP.Data.MySqlProvider.ExecuteScalar():" + ex.Message + "--" + sqlComm.CommandText);
                LastError = ex.Message;
            }
            return scalar;
        }

        public string ExecuteScalar(string strCommandText, MySqlParameter[] pars, CommandType commandType)
        {
            return ExecuteScalar(GetCommand(strCommandText, pars, commandType, 0));
        }

        public string ExecuteScalar(string strCommandText)
        {
            return ExecuteScalar(GetCommand(strCommandText));
        }
        #endregion

        #region ExecuteDataset
        /// <summary>
        /// 依据命令文本返回数据集
        /// </summary>
        /// <param name="sqlCommand"></param>
        /// <returns></returns>
        public DataSet ExecuteDataset(MySqlCommand sqlCommand)
        {
            var objDs = new DataSet();

            try
            {
                if (string.IsNullOrEmpty(sqlCommand.CommandText))
                {
                    LastError = "查询文本不可为空";
                    LogTool.WriteEntry("OUP.Data.MySqlProvider.ExecuteDataset():查询文本不可为空");
                }
                else using (var objDa = new MySqlDataAdapter(sqlCommand))
                    {
                        objDa.Fill(objDs);
                    }
            }
            catch (Exception ex)
            {
                LogTool.WriteEntry("OUP.Data.MySqlProvider.ExecuteDataset():" + ex.Message + "--" + sqlCommand.CommandText);
                LastError = ex.Message;
            }

            return objDs;
        }

        public DataSet ExecuteDataset(string strCommandText, MySqlParameter[] pars, CommandType commandType)
        {
            return (ExecuteDataset(GetCommand(strCommandText, pars, commandType, 0)));
        }

        public DataSet ExecuteDataset(string strCommandText)
        {
            return (ExecuteDataset(GetCommand(strCommandText)));
        }

        public DataSet ExecuteDataset(string strCommandText, int commandTimeout)
        {
            return (ExecuteDataset(GetCommand(strCommandText, commandTimeout)));
        }
        #endregion

        #region ExecuteTable
        /// <summary>
        /// 根据命令返回数据表
        /// </summary>
        /// <param name="sqlCommand"></param>
        /// <returns></returns>
        public DataTable ExecuteTable(MySqlCommand sqlCommand)
        {
            try
            {
                var objDt = new DataTable();
                var ds = ExecuteDataset(sqlCommand);
                if (ds.Tables.Count > 0)
                    objDt = ds.Tables[0];

                return objDt;
            }
            catch (Exception ex)
            {
                LogTool.WriteEntry("OUP.Data.MySqlProvider.ExecuteTable():" + ex.Message + "--" + sqlCommand.CommandText);
                LastError = ex.Message;

                return null;
            }
        }

        public DataTable ExecuteTable(string strCommandText, MySqlParameter[] pars, CommandType commandType)
        {
            var ds = ExecuteDataset(strCommandText, pars, commandType);
            return ds.Tables.Count > 0 ? ds.Tables[0] : null;
        }

        public DataTable ExecuteTable(string strCommandText)
        {
            var ds = ExecuteDataset(strCommandText);
            return ds.Tables.Count > 0 ? ds.Tables[0] : null;
        }

        public DataTable ExecuteTable(string strCommandText, int commandTimeout)
        {
            var ds = ExecuteDataset(strCommandText, commandTimeout);
            return ds.Tables.Count > 0 ? ds.Tables[0] : null;
        }

        #endregion

        #region ExecuteRow
        /// <summary>
        /// 根据命令返回数据行
        /// </summary>
        /// <param name="sqlCommand"></param>
        /// <returns></returns>
        public DataRow ExecuteRow(MySqlCommand sqlCommand)
        {
            try
            {
                var ds = ExecuteDataset(sqlCommand);
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    return ds.Tables[0].Rows[0];
                return null;
            }
            catch (Exception ex)
            {
                LogTool.WriteEntry("OUP.Data.MySqlProvider.ExecuteRow():" + ex.Message + "--" + sqlCommand.CommandText);
                LastError = ex.Message;
                return null;
            }
        }

        public DataRow ExecuteRow(string strCommandText, MySqlParameter[] pars, CommandType commandType)
        {
            var ds = ExecuteDataset(strCommandText, pars, commandType);
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                return ds.Tables[0].Rows[0];
            return null;
        }

        public DataRow ExecuteRow(string strCommandText)
        {
            var ds = ExecuteDataset(strCommandText);
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                return ds.Tables[0].Rows[0];
            return null;
        }

        public DataRow ExecuteRow(string strCommandText, int commandTimeout)
        {
            var ds = ExecuteDataset(strCommandText, commandTimeout);
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                return ds.Tables[0].Rows[0];
            return null;
        }
        #endregion

        #region ExecuteDataReader
        public MySqlDataReader ExecuteDataReader(MySqlCommand sqlComm)
        {
            try
            {
                if (sqlComm.Connection.State == ConnectionState.Closed)
                    sqlComm.Connection.Open();

                var obj = sqlComm.ExecuteReader();

                return obj;
            }
            catch (Exception ex)
            {
                LogTool.WriteEntry("OUP.Data.MySqlProvider.ExecuteDataReader():" + ex.Message + "--" + sqlComm.CommandText);
                LastError = ex.Message;
                return null;
            }
        }

        public MySqlDataReader ExecuteDataReader(string strCommandText, MySqlParameter[] pars, CommandType commandType)
        {
            return ExecuteDataReader(GetCommand(strCommandText, pars, commandType, 0));
        }

        public MySqlDataReader ExecuteDataReader(string strCommandText)
        {
            return ExecuteDataReader(GetCommand(strCommandText));
        }

        public MySqlDataReader ExecuteDataReader(string strCommandText, int commandTimeout)
        {
            return ExecuteDataReader(GetCommand(strCommandText, commandTimeout));
        }
        #endregion
    }
}
